/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UI.java
 *
 * Created on Feb 24, 2011, 9:01:01 PM
 */
package epaper;

import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;
import java.util.TimeZone;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Puneet
 */
public class UI extends javax.swing.JFrame implements ClipboardOwner, Observer {

    private String paper = null;
    private String language = "ENG";
    private int city = -1;
    private String fileName = null;
    private Date date = null;
    private static Date currDate = null;
    private ExecutorService es = null;
    private JFileChooser fc = new JFileChooser();
    private final String prefix = "Success : ";
    private String taskinprogress = "Fetching pages in parallel";

    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(Observable o, Object arg) {
        //System.out.println("called "+arg);
        incrementProgress((Integer) arg);
    }

    private enum PANE {

        HT, DNA, MINT, TOI,
    }
    private PANE activePane = PANE.HT;

    /** Creates new form UI */
    private UI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        jProgressBar.setString(EnglishNumberToWords.convert(0) + " percent");
        fc.setCurrentDirectory(new java.io.File("."));
        fc.setDialogTitle("Select a directory");
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by    the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jTextField2 = new javax.swing.JTextField();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        buttonGroup4 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLanguage = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jCity = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jDirectoryTF = new javax.swing.JTextField();
        jFileChooser = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jDateChooser = new com.toedter.calendar.JDateChooser();
        jPanel13 = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPaper = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jCopyFN = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jOpenFN = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jProgressBar = new javax.swing.JProgressBar();
        jResultBox = new javax.swing.JTextField();

        jTextField2.setText("jTextField2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "ePaper Downloader", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Comic Sans MS", 0, 16))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jPanel1.setMaximumSize(new java.awt.Dimension(310, 60));
        jPanel1.setPreferredSize(new java.awt.Dimension(310, 60));

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jLabel1.setText("Language : ");

        jLanguage.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jLanguage.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "English", "Hindi"}));
        jLanguage.setSelectedItem(0);
        jLanguage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLanguageActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jLabel7.setText("City : ");

        jCity.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jCity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Delhi", "Mumbai", "Chandigarh", "Kolkata" }));
        jCity.setSelectedItem(0);
        jCity.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCityActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jLabel4.setText("Directory : ");

        jDirectoryTF.setEditable(false);
        jDirectoryTF.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jDirectoryTF.setText(new File(".").getAbsolutePath());
        jDirectoryTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDirectoryTFActionPerformed(evt);
            }
        });

        jFileChooser.setText("jButton3");
        jFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFileChooserActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jLabel5.setText("Choose Date : ");

        jDateChooser.setDate(currDate);
        jDateChooser.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jDateChooser.setMaxSelectableDate(currDate);
        jDateChooser.setMinSelectableDate(new java.util.Date(1279094481000L));

        jPanel13.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel13.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel15.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jLabel15.setText("Developed By -- Punit Agrawal");
        jPanel13.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 10, -1, -1));

        jLabel16.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jLabel16.setText("Report errors @ punit.agrawal@wsu.edu");
        jPanel13.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, -1, -1));

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jLabel2.setText("Paper : ");

        jPaper.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jPaper.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hindustan Times", "DNA", "MINT", Constants.TOI}));
        jPaper.setSelectedItem(0);
        jPaper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPaperActionPerformed(evt);
            }
        });

        jCopyFN.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jCopyFN.setText("Copy Name");
        jCopyFN.setEnabled(false);
        jCopyFN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCopyFNActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jButton1.setText("Get ePaper");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jOpenFN.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jOpenFN.setText("Open File");
        jOpenFN.setEnabled(false);
        jOpenFN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOpenFNActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jButton2.setText("Exit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(jOpenFN, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCopyFN, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jCopyFN});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCopyFN, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jOpenFN, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2, jCopyFN});

        jProgressBar.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        jProgressBar.setMaximum(1000);
        jProgressBar.setBorderPainted(false);
        jProgressBar.setStringPainted(true);

        jResultBox.setEditable(false);
        jResultBox.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jResultBox.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jResultBox.setAutoscrolls(false);
        jResultBox.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Result", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Comic Sans MS", 0, 12))); // NOI18N
        jResultBox.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jProgressBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jResultBox, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jResultBox, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jDateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jCity, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jLanguage, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jPaper, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jDirectoryTF, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jPanel13, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel4, jLabel5, jLabel7});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPaper, javax.swing.GroupLayout.PREFERRED_SIZE, 23, Short.MAX_VALUE))
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 3, Short.MAX_VALUE)
                        .addComponent(jLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jDirectoryTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel13, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jCity, jDateChooser, jDirectoryTF, jFileChooser, jLabel1, jLabel2, jLabel4, jLabel5, jLabel7});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCopyFNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCopyFNActionPerformed
        // TODO add your handling code here:
        /*Runtime rt = Runtime.getRuntime();
        System.out.println("start "+jResultBox.getText().substring(prefix.length()));
        try {
        if (OSUtils.isWindows()) {
        rt.exec("start "+jResultBox.getText().substring(prefix.length()));
        } else if (OSUtils.isUnix()) {
        rt.exec("evince " + jResultBox.getText().substring(prefix.length()));
        }
        } catch (IOException ex) {
        Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(jResultBox.getText().substring(prefix.length())), this);
        new Thread(new Runnable() {

            public void run() {
                try {
                    String text = jResultBox.getText();
                    jResultBox.setText("Filename copied to Clipboard !!");
                    Thread.sleep(3000);
                    jResultBox.setText(text);
                } catch (InterruptedException ex) {
                    Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();

    }//GEN-LAST:event_jCopyFNActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        // TODO add your handling code here:

        jCopyFN.setEnabled(false);
        jOpenFN.setEnabled(false);
        setTaskInProgress("Fetching pages in parallel");
        setProgress(0);

        if (activePane == PANE.HT) {
            if (language == null) {
                jResultBox.setText("Please specify language");
                return;
            }
        }
        if (jCity.getSelectedIndex() == -1 && !(activePane == PANE.MINT)) {
            jResultBox.setText("Please specify city");
            return;
        } else if (!(activePane == PANE.MINT)) {
            try {
                city = Cities.getInstance().getClass().getField(((String) jCity.getSelectedItem()).toUpperCase()).getInt(Cities.getInstance());
                System.out.println("selected city : " + city);
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        date = jDateChooser.getDate();


        if (activePane == PANE.MINT && date != null) {
            if (date.getDay() == 0) {
                jResultBox.setText("MINT paper not available on Sundays :(");
                return;
            }
        }

        if (date == null) {
            jResultBox.setText("Please specify date");
            return;
        }

        fileName = jDirectoryTF.getText();
        if (fileName.length() < 1) {
            jResultBox.setText("Please specify file name");
            return;
        }
        jResultBox.setText("< " + taskinprogress + " >");
        Thread t = new Thread(new Runnable() {

            public void run() {
                try {
                    es = Executors.newCachedThreadPool();
                    Callable<String> call = null;
                    if (activePane == PANE.HT) {
                        call = new HT(language, city, fileName, date);
                    } else if (activePane == PANE.DNA) {
                        call = new DNAMain(city, fileName, date);
                    } else if (activePane == PANE.MINT) {
                        call = new Mint(fileName, date);
                    } else if (activePane == PANE.TOI) {
                        call = new TOI(city, fileName, date);
                    }
                    Future<String> fut = es.submit(call);
                    String get = null;
                    while (get == null) {
                        try {
                            get = fut.get(200, TimeUnit.MILLISECONDS);
                        } catch (TimeoutException ex) {
                            //Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
                            if (jResultBox.getText().length() < 38) {
                                jResultBox.setText("<" + jResultBox.getText() + ">");
                            } else {
                                jResultBox.setText("< " + taskinprogress + " >");
                            }
                        }
                    }
                    jResultBox.setText(prefix + get);
                    jResultBox.setEnabled(true);
                    jResultBox.setEditable(true);


                } catch (InterruptedException ex) {
                    Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
                    jResultBox.setText("Error Occurred");
                } catch (ExecutionException ex) {
                    Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
                    jResultBox.setText("Error Occurred");

                } finally {
                    es.shutdown();
                    jResultBox.setEditable(true);
                    jCopyFN.setEnabled(true);
                    jOpenFN.setEnabled(true);
                }
            }
        });
        t.start();
        //Runnable job = new Main(language, city, fileName);

        //SwingUtilities.invokeLater(job);


        return;


    }//GEN-LAST:event_jButton1ActionPerformed

    public void setTaskInProgress(String tip) {
        this.taskinprogress = tip;
        setProgress(1000);
    }

    private void jFileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFileChooserActionPerformed
        // TODO add your handling code here:
        fc.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                jDirectoryTF.setText(fc.getSelectedFile().getCanonicalPath());
                //This is where a real application would open the file.
            } catch (IOException ex) {
                Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            if (jDirectoryTF.getText().length() == 0) {
                jResultBox.setText("Error in selecting file");
            }
        }
}//GEN-LAST:event_jFileChooserActionPerformed

    private void jCityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCityActionPerformed
        JComboBox cb = (JComboBox) evt.getSource();
        if (cb.getSelectedIndex() != -1) {
            try {
                city = Cities.getInstance().getClass().getField(((String) cb.getSelectedItem()).toUpperCase()).getInt(Cities.getInstance());
                /*if(((String)cb.getSelectedItem()).equalsIgnoreCase("Delhi")){
                city = Cities.DELHI;
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Mumbai")){
                city = Cities.MUMBAI;
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Chandigarh")){
                city = "CHA";
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Kolkata")){
                city = "KOL";
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Patna")){
                city = "PAT";
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Lucknow")){
                city = "LUC";
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Ranchi")){
                city = "RAN";
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Ahmedabad")){
                city = "AHM";
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Jaipur")){
                city = "JAI";
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("Pune")){
                city = "PUN";
                }else if(((String)cb.getSelectedItem()).equalsIgnoreCase("bangalore")){
                city = "BAN";
                }
                 *
                 */
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
}//GEN-LAST:event_jCityActionPerformed

    private void jLanguageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLanguageActionPerformed
        // TODO add your handling code here:
        JComboBox cb = (JComboBox) evt.getSource();
        if (cb.getSelectedIndex() != -1) {
            if (((String) cb.getSelectedItem()).equalsIgnoreCase("English") && ((String) jPaper.getSelectedItem()).equalsIgnoreCase("Hindustan Times")) {
                language = "ENG";
                jCity.setSelectedIndex(-1);
                jCity.removeAllItems();
                jCity.addItem("Delhi");
                jCity.addItem("Mumbai");
                jCity.addItem("Chandigarh");
                jCity.addItem("Kolkata");
                jCity.setSelectedIndex(0);

            } else if (((String) cb.getSelectedItem()).equalsIgnoreCase("Hindi") && ((String) jPaper.getSelectedItem()).equalsIgnoreCase("Hindustan Times")) {
                language = "HIN";
                jCity.setSelectedIndex(-1);
                jCity.removeAllItems();
                jCity.addItem("Delhi");
                jCity.addItem("Patna");
                jCity.addItem("Lucknow");
                jCity.addItem("Ranchi");
                jCity.setSelectedIndex(0);
            } else if (((String) cb.getSelectedItem()).equalsIgnoreCase("English") && ((String) jPaper.getSelectedItem()).equalsIgnoreCase("DNA")) {
                language = "ENG";
                jCity.setSelectedIndex(-1);
                jCity.removeAllItems();
                jCity.addItem("Mumbai");
                jCity.addItem("Ahmedabad");
                jCity.addItem("Pune");
                jCity.addItem("Jaipur");
                jCity.addItem("Bangalore");
                jCity.setSelectedIndex(0);
            } else if (((String) cb.getSelectedItem()).equalsIgnoreCase("English") && ((String) jPaper.getSelectedItem()).equals(Constants.TOI)) {
                language = "ENG";
                jCity.setSelectedIndex(-1);
                jCity.removeAllItems();
                jCity.addItem("Mumbai");
                jCity.addItem("Ahmedabad");
                jCity.addItem("Bangalore");
                jCity.addItem("Chennai");
                jCity.addItem("Delhi");
                jCity.addItem("Hyderabad");
                jCity.addItem("Jaipur");
                jCity.addItem("Kolkata");
                jCity.addItem("Lucknow");
                jCity.addItem("Pune");
                jCity.setSelectedIndex(0);
            }
        }
}//GEN-LAST:event_jLanguageActionPerformed

    private void jPaperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPaperActionPerformed
        // TODO add your handling code here:
        JComboBox cb = (JComboBox) evt.getSource();
        if (((String) cb.getSelectedItem()).equalsIgnoreCase("Hindustan Times")) {
            activePane = PANE.HT;
            jLanguage.removeAllItems();
            jLanguage.addItem("English");
            jLanguage.addItem("Hindi");
            jLanguage.setSelectedIndex(0);
            language = "ENG";

            jCity.setSelectedIndex(-1);
            jCity.removeAllItems();
            jCity.addItem("Delhi");
            jCity.addItem("Mumbai");
            jCity.addItem("Chandigarh");
            jCity.addItem("Kolkata");
            jCity.setSelectedIndex(0);
            jCity.setEnabled(true);
            //city = "DEL";
        } else if (((String) cb.getSelectedItem()).equalsIgnoreCase("DNA")) {
            activePane = PANE.DNA;
            jLanguage.setSelectedIndex(-1);
            jLanguage.removeAllItems();
            jLanguage.addItem("English");
            jLanguage.setSelectedIndex(0);
            language = "ENG";

            jCity.setSelectedIndex(-1);
            jCity.removeAllItems();
            jCity.addItem("Mumbai");
            jCity.addItem("Ahmedabad");
            jCity.addItem("Pune");
            jCity.addItem("Jaipur");
            jCity.addItem("Bangalore");
            jCity.setSelectedIndex(0);
            jCity.setEnabled(true);
            //city = "MUM";
        } else if (((String) cb.getSelectedItem()).equalsIgnoreCase("MINT")) {
            activePane = PANE.MINT;
            jLanguage.setSelectedIndex(-1);
            jLanguage.removeAllItems();
            jLanguage.addItem("English");
            jLanguage.setSelectedIndex(0);
            language = "ENG";

            jCity.setSelectedIndex(-1);
            jCity.setEnabled(false);

        } else if (((String) cb.getSelectedItem()).equals(Constants.TOI)) {
            activePane = PANE.TOI;

            jLanguage.setSelectedIndex(-1);
            jLanguage.removeAllItems();
            jLanguage.addItem("English");
            jLanguage.setSelectedIndex(0);
            language = "ENG";


            jCity.setSelectedIndex(-1);
            jCity.removeAllItems();
            jCity.addItem("Mumbai");
            jCity.addItem("Ahmedabad");
            jCity.addItem("Bangalore");
            jCity.addItem("Chennai");
            jCity.addItem("Delhi");
            jCity.addItem("Hyderabad");
            jCity.addItem("Jaipur");
            jCity.addItem("Kolkata");
            jCity.addItem("Lucknow");
            jCity.addItem("Pune");
            jCity.setSelectedIndex(0);


        }
}//GEN-LAST:event_jPaperActionPerformed

    private void jDirectoryTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDirectoryTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jDirectoryTFActionPerformed

    public void incrementProgress(int progress) {


        jProgressBar.setValue(jProgressBar.getValue() + progress);
        int x = jProgressBar.getValue()/10;
        System.out.println(x);
        jProgressBar.setString(EnglishNumberToWords.convert(x) + " percent");
        jProgressBar.repaint();
    }

    public void setProgress(int progress) {
        jProgressBar.setValue(progress);
        int x = progress/10;
        jProgressBar.setString(EnglishNumberToWords.convert(x) + " percent");
    }

    private void jOpenFNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOpenFNActionPerformed
        try {
            // TODO add your handling code here:
            String file = jResultBox.getText().substring(prefix.length());
            file.replace('\\', '/');
            System.out.println(file);
            //if(OSUtils.isWindows())
            //    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
            Desktop dOpen = Desktop.getDesktop();
            dOpen.open(new File(file));


        } catch (IOException ex) {
            Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jOpenFNActionPerformed
    /**
     * @param args the command line arguments
     */
    public static UI myUI = null;

    public static UI getInstance() {
        if (myUI == null) {
            myUI = new UI();
        }
        return myUI;
    }

    public static Date getTime(String timeZoneId) {
        Calendar calTZ = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
        calTZ.setTimeInMillis(new Date().getTime());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, calTZ.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, calTZ.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, calTZ.get(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, calTZ.get(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, calTZ.get(Calendar.MINUTE));
        cal.set(Calendar.SECOND, calTZ.get(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, calTZ.get(Calendar.MILLISECOND));

        return cal.getTime();
    }

    public static void main(String args[]) {

        TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
        currDate = getTime(tz.getID());
        System.out.println(currDate);
        java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
        UI myUI = getInstance();
        myUI.setVisible(true);
        }
        });
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jCity;
    private javax.swing.JButton jCopyFN;
    private com.toedter.calendar.JDateChooser jDateChooser;
    private javax.swing.JTextField jDirectoryTF;
    private javax.swing.JButton jFileChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JComboBox jLanguage;
    private javax.swing.JButton jOpenFN;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JComboBox jPaper;
    private javax.swing.JProgressBar jProgressBar;
    private javax.swing.JTextField jResultBox;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
